package org.apereo.cas.config;

import com.sso.webflow.SomethingWebflowConfigurer;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.web.flow.CasWebflowConfigurer;
import org.apereo.cas.web.flow.CasWebflowExecutionPlan;
import org.apereo.cas.web.flow.CasWebflowExecutionPlanConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.webflow.definition.registry.FlowDefinitionRegistry;
import org.springframework.webflow.engine.builder.support.FlowBuilderServices;

/**
 * 类 {@code SomethingConfiguration}  <br> 一些我们自定义的配置.
 *
 * <p>详细描述
 * <p>
 * @author <a href="mailto:lz2392504@gmail.com">CN華少</a>
 * @see CasWebflowExecutionPlanConfigurer
 * @since v1.0.0
 */
@Configuration("somethingConfiguration")
@EnableConfigurationProperties(CasConfigurationProperties.class)
public class SomethingConfiguration implements CasWebflowExecutionPlanConfigurer {

    /**
     * CAS 配置
     */
    @Autowired
    private CasConfigurationProperties casProperties;

    /**
     * 登录流程定义注册表
     */
    @Autowired
    @Qualifier("loginFlowRegistry")
    private FlowDefinitionRegistry loginFlowDefinitionRegistry;

    /**
     * 应用关系
     */
    @Autowired
    private ConfigurableApplicationContext configurableApplicationContext;

    /**
     * 流建设者服务
     */
    @Autowired
    private FlowBuilderServices flowBuilderServices;

    /*@ConditionalOnMissingBean(name = "somethingWebflowConfigurer")*/
    @Bean
    public CasWebflowConfigurer somethingWebflowConfigurer() {
        System.out.println("初始化配置");
        final SomethingWebflowConfigurer somethingWebflowConfigurer = new SomethingWebflowConfigurer(flowBuilderServices,
                loginFlowDefinitionRegistry, configurableApplicationContext, casProperties);
        somethingWebflowConfigurer.initialize();
        return somethingWebflowConfigurer;
    }

    @Override
    public void configureWebflowExecutionPlan(CasWebflowExecutionPlan plan) {
        plan.registerWebflowConfigurer(somethingWebflowConfigurer());
    }
}
